Imports Pacientes
Imports System.Data.SqlClient
Public Class frmCalcularMontos
    Private objTurno As Turnos()
    Private prof As Profesional = Nothing
    Private datos As DataTable = Nothing
    Private destino As DataSet = New DataSet()
    Private origen As DataSet = New DataSet()
    Private nom As Nomenclador = Nothing
    Private dblMonto As Double
    Private TipoEst As TipoEstudio = Nothing
    Private paci As Paciente = Nothing
    Private Estud As Estudio = Nothing
    Private tipodoc As Integer = 0
    Private numdoc As String = ""
    Private idTipoEst As Integer = 0
    Private DescTE As String = ""
    Private idEst As Integer = 0
    Private fia As Integer = 0
    Private profes As String = ""
    Private banderita As Integer = 0
    Private Desc As String = ""
    Private fecGFia As Date = Nothing
    Private fecEFia As Date = Nothing
    Private idEstado As Integer = 0
    Private matProf As Integer = 0
    Private c As Long = 0

    Public Property Paciente() As Paciente
        Get
            Return paci
        End Get
        Set(ByVal value As Paciente)
            paci = value
        End Set
    End Property
    Public Property Monto()
        Get
            Return dblMonto
        End Get
        Set(ByVal value)
            dblMonto = value
        End Set
    End Property
    Sub CrearDestino()
        datos = New DataTable("PROV_DESTINO")
        datos.Columns.Add(New DataColumn("codigo"))
        datos.Columns.Add(New DataColumn("descripcion"))
        datos.Columns.Add(New DataColumn("DescripTipo"))
        datos.Columns.Add(New DataColumn("precio"))

        destino.Tables.Add(datos)
        grAnalisisAsignados.DataSource = destino
        grAnalisisAsignados.DataMember = "prov_destino"
    End Sub

    Private Sub SelecAnalisis_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Dim paci As Paciente = New Paciente()
        Dim TipoPac As TipoPaciente = New TipoPaciente()
        If banCalMon = 1 Then
            txtTipoDoc.Text = TurnosCORREGIDO.cboTipo.Text
            txtNroDoc.Text = TurnosCORREGIDO.txtNroDoc.Text
            txtNombre1.Text = TurnosCORREGIDO.txtApe.Text + " " + TurnosCORREGIDO.txtNom.Text

            cboProfesional.Text = TurnosCORREGIDO.lblProfTurno.Text


            If txtTipoDoc.Text = "DNI" Then
                tipodoc = 1
            End If
            If txtTipoDoc.Text = "LC" Then
                tipodoc = 2
            End If
            If txtTipoDoc.Text = "CI" Then
                tipodoc = 3
            End If
            If txtTipoDoc.Text = "LE" Then
                tipodoc = 4
            End If

            numdoc = txtNroDoc.Text

            paci.Buscar(tipodoc, numdoc, conexion)

            If paci.TipoPaciente.Descripcion = "Particular" Then
                fraObraSocial.Enabled = False
            Else
                fraObraSocial.Enabled = True
                txtNomOS.Text = paci.ObraSocialP.DescripOS
                txtPlan.Text = paci.NomPlanPac
                txtNroAfiliado.Text = paci.NroAfiliadoP
            End If
            
        Else
            cargarComboTipoEstudio()
            txtRequisito.Text = ""
            cmdAnalisis.Enabled = False
            CargarProfesional()
        End If
    End Sub
    'Private Sub ModiEstadoTur()
    '    Dim objTurnoAsignado As TurnosxPaciente = New TurnosxPaciente()
    '    objTurnoAsignado.idhoraTP = TurnosCORREGIDO.txtNroTurno.Text
    '    objTurnoAsignado.fechaTP = Format(TurnosCORREGIDO.dtpFechaTur.Value, "dd/MM/yyyy")
    '    Dim id As Integer = 0
    '    If txtTipoDoc.Text = "DNI" Then
    '        id = 1
    '    End If
    '    If txtTipoDoc.Text = "LC" Then
    '        id = 2
    '    End If
    '    If txtTipoDoc.Text = "CI" Then
    '        id = 3
    '    End If
    '    If txtTipoDoc.Text = "LE" Then
    '        id = 4
    '    End If
    '    If txtTipoDoc.Text = "MAT.PROF" Then
    '        id = 5
    '    End If

    '    numdoc = txtNroDoc.Text


    '    objTurnoAsignado.tipodocTP = id 'Paciente.tipodocumentoPer
    '    objTurnoAsignado.nrodocTP = numdoc ' Paciente.NroDocumentoPer
    '    objTurnoAsignado.estado = 7
    '    'DATOS PROFESIONAL
    '    Dim prof As Profesional = Nothing
    '    prof = New Profesional()
    '    profes = TurnosCORREGIDO.lblProfTurno.Text ' txtProfTurno.Text
    '    With Me.prof
    '        If prof.BuscarProf(profes, conexion) Then
    '            objTurnoAsignado.matri = prof.MatriculaProf
    '        End If
    '    End With
    '    objTurnoAsignado.NroTur = TurnosCORREGIDO.txtNroTurno.Text

    '    Dim idEstadoTur As Integer = 0
    '    idEstadoTur = 7
    '    objTurnoAsignado.ModiEstado(idEstadoTur, conexion)

    'End Sub
    Private Sub cmdAgregar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdAgregar.Click
        Dim strCodigo As String
        Dim dr As DataRow
        Dim analisis As Integer = 0
        strCodigo = CStr(grAnalisisCargados.SelectedCells(0).Value)

        Dim objNomenclador As Nomenclador = New Nomenclador
        objNomenclador.Buscar(CInt(strCodigo), conexion)

        'Fila Destino
        dr = destino.Tables("PROV_DESTINO").NewRow()
        dr.Item("codigo") = objNomenclador.CodigoAnalisis
        dr.Item("descripcion") = objNomenclador.NombreAnalisis
        dr.Item("DescripTipo") = objNomenclador.descripcionTipo
        dr.Item("precio") = objNomenclador.PrecioNomenclador
        destino.Tables("PROV_DESTINO").Rows.Add(dr)
        analisis = objNomenclador.CodigoAnalisis
        'Monto
        Me.Monto = Me.Monto + objNomenclador.PrecioNomenclador

        Me.Actualizar()

        Dim objFia As Fia = New Fia()
        Dim objFiaEst As FiaEstudios = New FiaEstudios()
        Dim objTur As TurnosxPaciente = New TurnosxPaciente()
        Dim idEstadoTur As Integer = 0

        'REGISTRAR ESTRUC-FIA
        If banderita = 0 Then
            banderita = 1

            'ModiEstadoTur()

            Dim id As Integer = 0
            Dim numDoc As String = ""

            If txtTipoDoc.Text = "DNI" Then
                id = 1
            End If
            If txtTipoDoc.Text = "LC" Then
                id = 2
            End If
            If txtTipoDoc.Text = "CI" Then
                id = 3
            End If
            If txtTipoDoc.Text = "LE" Then
                id = 4
            End If
            If txtTipoDoc.Text = "MAT.PROF" Then
                id = 5
            End If

            Randomize()
            c = Int((10000 * Rnd())) ' para numero entero

            objFia.nrofia = c
            objFia.tipodocFia = id
            objFia.nroDocFia = txtNroDoc.Text
            objFia.FecGenFia = Now
            objFia.idEstF = 6
            'cargar matricula del Profesional
            Dim prof As Profesional = Nothing
            prof = New Profesional()
            profes = cboProfesional.Text
            With Me.prof
                If prof.BuscarProf(profes, conexion) Then
                    objFia.matricula = prof.MatriculaProf
                End If
            End With
            'COMO CALCULAR FECHA DE ENTREGA
            Dim FechaSql1 As String = Now
            Dim Fecha1 As Date
            Fecha1 = CDate(FechaSql1)
            Dim Suma1 As Date = Fecha1.AddDays(1)

            objFia.fecEntregaFia = Format(Suma1, "dd/MM/yyyy")

            ''asignacion para la modificacion del monto
            'fia = c
            'tipodoc = id
            'numDoc = txtNroDoc.Text
            'fecGFia = Now
            'fecEFia = objFia.fecEntregaFia
            'matProf = objFia.matricula
            'idEstado = 6 'estado pendiente

            objFia.Agregar(conexion)

            '' para registrar detalle FIA
            Dim Estud As Estudio = Nothing
            Estud = New Estudio()
            Desc = cboEstudio.Text
            With Me.Estud
                If Estud.BuscarId(Desc, conexion) Then
                    idEst = Estud.Identificador
                End If
            End With

            
            objFiaEst.numfia = c
            objFiaEst.estudioFia = idEst
            objFiaEst.AnalisisFia = analisis
            objFiaEst.ResuFia = ""
            objFiaEst.resu2F = ""

            If objNomenclador.Buscar(analisis, conexion) Then
                objFiaEst.categoria = objNomenclador.descripcionTipo
            End If
            objFiaEst.observF = ""
            objFiaEst.calificF = ""
            objFiaEst.LugarMFia = ""

            objFiaEst.Agregar(conexion)
        Else
            'REG.SOLO DETALLE DE FIA 
            Dim Estud As Estudio = Nothing
            Estud = New Estudio()
            Desc = cboEstudio.Text
            With Me.Estud
                If Estud.BuscarId(Desc, conexion) Then
                    idEst = Estud.Identificador
                End If
            End With
            objFiaEst.numfia = c
            objFiaEst.estudioFia = idEst
            objFiaEst.AnalisisFia = analisis
            objFiaEst.ResuFia = ""
            objFiaEst.resu2F = ""
            If objNomenclador.Buscar(analisis, conexion) Then
                objFiaEst.categoria = objNomenclador.descripcionTipo
            End If
            objFiaEst.observF = ""
            objFiaEst.calificF = ""
            objFiaEst.LugarMFia = ""
            objFiaEst.Agregar(conexion)
        End If
        Me.grAnalisisCargados.Rows.Remove(Me.grAnalisisCargados.CurrentRow)


    End Sub

    Private Sub Actualizar()
        txtMonto.Text = CStr(Me.Monto)
        grAnalisisAsignados.Refresh()
    End Sub
    Private Sub cmdQuitar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdQuitar.Click
        For Each dr As System.Windows.Forms.DataGridViewRow In grAnalisisAsignados.SelectedRows
            Me.Monto = Me.Monto - CDbl(dr.Cells(2).Value)
            grAnalisisAsignados.Rows.RemoveAt(dr.Index)

        Next

        Me.Actualizar()

    End Sub
    Private Sub CargarProfesional()
        Dim datos As DataSet = Nothing
        prof = New Profesional()
        datos = prof.BuscarDatos(conexion)
        Me.cboProfesional.DataSource = datos.Tables(0)
        Me.cboProfesional.DisplayMember = "apenom"
        Me.cboProfesional.ValueMember = "matricula"

        Dim index As Integer = 0
        index = Me.cboProfesional.FindString(Me.cboProfesional.Text)
        Me.cboProfesional.SelectedIndex = index
        cboProfesional.Text = ""
    End Sub
    Private Sub cargarComboTipoEstudio()
        Dim datos As DataSet = Nothing

        TipoEst = New TipoEstudio()
        datos = TipoEst.BuscarDatos(conexion)
        Me.cboTEstud.DataSource = datos.Tables(0)
        Me.cboTEstud.DisplayMember = "Descrip"
        Me.cboTEstud.ValueMember = "IdTipoEstudio"

        Dim index As Integer = 0
        index = Me.cboTEstud.FindString(Me.cboTEstud.Text)
        Me.cboTEstud.SelectedIndex = index

        Me.cboTEstud.Text = ""
        txtRequisito.Text = ""
    End Sub
    Private Sub cargarComboEstudio()
        Dim datos As DataSet = Nothing
        Dim TipoEst As TipoEstudio = Nothing
        TipoEst = New TipoEstudio()
        DescTE = cboTEstud.Text
        With Me.TipoEst
            If TipoEst.BuscarId(DescTE, conexion) Then
                idTipoEst = TipoEst.TipoEstudio
            End If
        End With

        Dim Estud As Estudio = Nothing
        Estud = New Estudio()
        datos = Estud.BuscarEst(idTipoEst, conexion)
        Me.cboEstudio.DataSource = datos.Tables(0)
        Me.cboEstudio.DisplayMember = "nombre"
        Me.cboEstudio.ValueMember = "IdEstudio"

        Dim index As Integer = 0
        index = Me.cboEstudio.FindString(Me.cboEstudio.Text)
        Me.cboEstudio.SelectedIndex = index

        idEst = Estud.Identificador
        cboEstudio.Text = ""

    End Sub
    Private Sub cmdAceptar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdAceptar.Click
        'Dim objMod As Fia = Nothing

        'objMod = New Fia()

        'Dim id As Integer = 0
        'Dim numDoc As String = ""

        'If txtTipoDoc.Text = "DNI" Then
        '    id = 1
        'End If
        'If txtTipoDoc.Text = "LC" Then
        '    id = 2
        'End If
        'If txtTipoDoc.Text = "CI" Then
        '    id = 3
        'End If
        'If txtTipoDoc.Text = "LE" Then
        '    id = 4
        'End If
        'If txtTipoDoc.Text = "MAT.PROF" Then
        '    id = 5
        'End If
        'objMod.MonEst = txtMonto.Text
        'objMod.nroDocFia = txtNroDoc.Text
        'objMod.tipodocFia = id
        ''objMod.resultado = 0
        objMod.ModiMonto(fia, conexion)
        Close()
    End Sub

    Private Sub Salir_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Salir.Click
        Close()
    End Sub

    Private Sub cmdBuscar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdBuscar.Click
        banCalMon = 2
        Dim tipDoc As TipoDocumento = Nothing
        Dim barrio As Barrio = Nothing
        Dim frm As SelecPaciente = New SelecPaciente()
        frm.ShowDialog(Me)
        frm.Dispose()

        With Me.Paciente
            txtNombre1.Text = .NombrePer + " " + .ApellidoPer
            txtNroDoc.Text = .NroDocumentoPer

            tipDoc = New TipoDocumento()
            If tipDoc.Buscar(.tipodocumentoPer, conexion) Then
                txtTipoDoc.Text = tipDoc.Descripcion
            End If
            If Paciente.ObraSocialP.DescripOS <> "" Then
                txtNomOS.Text = .ObraSocialP.DescripOS
                txtPlan.Text = .NomPlanPac
                txtNroAfiliado.Text = .NroAfiliadoP
            End If
        End With
        cargarProfesional()
    End Sub
    Function SoloNumeros(ByVal Keyascii As Short) As Short
        If InStr("1234567890", Chr(Keyascii)) = 0 Then
            SoloNumeros = 0
        Else
            SoloNumeros = Keyascii
        End If
        Select Case Keyascii
            Case 8
                SoloNumeros = Keyascii
            Case 13
                SoloNumeros = Keyascii
        End Select
    End Function

    Private Sub txtNroDoc_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtNroDoc.KeyPress
        Dim KeyAscii As Short = CShort(Asc(e.KeyChar))
        KeyAscii = CShort(SoloNumeros(KeyAscii))
        If KeyAscii = 0 Then
            e.Handled = True
        End If
    End Sub

    Private Sub cboTEstud_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboTEstud.SelectedIndexChanged
        cargarComboEstudio()
        txtRequisito.Text = ""
    End Sub

    Private Sub RequisitosEstudio()
        Dim descEst As String = ""
        Dim Estud As Estudio = Nothing
        Estud = New Estudio()
        descEst = cboEstudio.Text
        With Me.Estud
            Estud.BuscarId(descEst, conexion)
            txtRequisito.Text = Estud.RequisitoEstudio
            idEst = Estud.Identificador
        End With
    End Sub

    Private Sub cboEstudio_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboEstudio.SelectedIndexChanged
        RequisitosEstudio()
        cmdAnalisis.Enabled = True
    End Sub
    Private Sub GrillaAnalisis()
        grAnalisisAsignados.AutoGenerateColumns = False
        grAnalisisCargados.AutoGenerateColumns = False
        ' cargarComboTipoEstudio()

        Me.MaximizeBox = False
        Me.MinimizeBox = False

        CrearDestino()
        nom = New Nomenclador()
        origen = nom.BuscarDatos(idEst, conexion)
        grAnalisisCargados.DataSource = origen
        grAnalisisCargados.DataMember = "nomenclador"
       


    End Sub
    Private Sub GrillaAnalisis2()
        Dim datos As DataSet = Nothing
        nom = New Nomenclador()
        datos = nom.BuscarDatos(idEst, conexion)
        grAnalisisAsignados.AutoGenerateColumns = False
        grAnalisisCargados.AutoGenerateColumns = False

        Me.MaximizeBox = False
        Me.MinimizeBox = False

        grAnalisisAsignados.AutoGenerateColumns = False
        grAnalisisAsignados.DataSource = datos
        grAnalisisAsignados.DataMember = "nomenclador"
    End Sub
    
    Private Sub cmdAnalisis_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdAnalisis.Click
        GrillaAnalisis()
        cmdAnalisis.Enabled = False

    End Sub

    'Private Sub chkCompleto_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles chkCompleto.Click
    '    If chkCompleto.Checked = True Then
    '        GrillaAnalisis2()
    '    Else
    '        ' LimpiarGrilla2()
    '    End If

    'End Sub

    'Private Sub grAnalisisCargados_CellClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs)
    '    'If grAnalisisCargados.Columns(e.ColumnIndex).Name = "colCheck1" Then
    '    Dim id As Integer = 0
    '    Dim nom As Nomenclador = New Nomenclador

    '    Dim celda As DataGridViewButtonCell = _
    '        CType(Me.grAnalisisCargados.Rows(e.RowIndex).Cells("colCheck1"), DataGridViewButtonCell)

    '    id = Integer.Parse(grAnalisisCargados.Rows(e.RowIndex).Cells("idanalisis").Value) 'da error dice que es nulo

    '    Dim datos As DataSet = Nothing
    '    nom = New Nomenclador()
    '    datos = nom.BuscarId(id, conexion)
    '    grAnalisisAsignados.AutoGenerateColumns = False
    '    grAnalisisCargados.AutoGenerateColumns = False

    '    Me.MaximizeBox = False
    '    Me.MinimizeBox = False

    '    grAnalisisAsignados.AutoGenerateColumns = False
    '    grAnalisisAsignados.DataSource = datos
    '    grAnalisisAsignados.DataMember = "nomenclador"


    '    'Close()


    '    ' End If
    'End Sub

    
End Class